// shop/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    goodslist:[],
    inputValue:"",
    noneview:true,
    tips:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     wx.request({
        url: 'https://www.prebeauty.work:3001/api/paihangbang',
        header:{
          'content-type':'application/json'
        },
        success : res => {
          console.log(res.data);
          this.setData({
            list:res.data
          })
        }
      })

      this.getSearchResult("");
  },

  getSearchResult(keyword) {
    if(this.data.inputValue){
      wx.request({
        url: 'https://www.prebeauty.work:3001/api/goods?keyword='+ this.data.inputValue,
        header: {
          'content-type': 'application/json'
        },
        success: this.getSearchResultSucc.bind(this)
      })
    }
  },

  getSearchResultSucc(res) {
    console.log(res.data)
    if (res.data&&this.data.noneview) {
     const result = res.data;
     console.log(result)
     this.setData({
      goodslist: result,
      noneview:false,
      tips:''
     })
    } else {
     this.setData({
      goodslist: [] ,
      noneview:true,
      tips:'抱歉，这个礼品还没有哦'
     })
    }
   },

   contentSaveTime:0,
   handleInputChange:function(e) {
     this.setData({
       inputValue:e.detail.value
     })
    let tm = Date.now();
    if((this.contentSaveTime+500)>tm){
      return;
    }
    this.contentSaveTime=tm;
    console.log('获取输入',e.detail.value);
    //实时缓存表单输入
    wx.setStorageSync('home-value', encodeURIComponent(e.detail.content));
   },

   handleSearch (keyword) {
    this.getSearchResult(keyword)
    // console.log(this.data)
   },

   handleItemTap(e) {
     console.log(e.currentTarget)
    wx.navigateTo({
     url: '/shop/good/good?goodsid=' + e.currentTarget.dataset.goodsid
    })
   }
})